package com.share51.novel.job;

import com.share51.novel.common.StatusConstant;
import com.share51.novel.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import javax.annotation.Resource;

/**
 * 布隆过滤器定时任务 定时重新初始化布隆过滤器 防止内存溢出
 * @author gb
 */
@Slf4j
@DisallowConcurrentExecution
public class BloomFilterJob extends QuartzJobBean {

    @Resource
    private TaskService taskService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        boolean running = jobExecutionContext
                .getTrigger()
                .getJobDataMap()
                .getBoolean(StatusConstant.QUARTZ_RUNNING_STATUS);
        if (!running){
            jobExecutionContext
                    .getTrigger()
                    .getJobDataMap()
                    .put(StatusConstant.QUARTZ_RUNNING_STATUS,true);
            try {
                log.info("开始重新初始化布隆过滤器");
                taskService.initBloomFilter();
                log.info("重新初始化布隆过滤器完成");
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                jobExecutionContext
                        .getTrigger()
                        .getJobDataMap()
                        .put(StatusConstant.QUARTZ_RUNNING_STATUS,false);
            }
        }
    }
}
